package com.example.FirstBook.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.solver.LinearSystem;

import com.example.FirstBook.Bean.FoodBean;
import com.example.FirstBook.adapter.FootListItemAdapter;
import com.example.FirstBook.util.FoodUtil;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

/*
食物搭配表
 */
public class food_list_Activity extends AppCompatActivity {

    private ImageView imageClearBtn;
    private ImageView imageSearchBtn;
    private EditText footActivityEdit;
    private ListView footListview;
    List<FoodBean>datas=new ArrayList<>();
    List<FoodBean>foodBeanList=new ArrayList<>();
    FoodBean foodBean;
    FootListItemAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list_);
        //隐藏标题栏
        ActionBar actionBar=getSupportActionBar();
        if (actionBar!=null){
            actionBar.hide();
        }
        initView();
        datas=FoodUtil.getFoodList();
        adapter=new  FootListItemAdapter(this,datas);
        footListview.setAdapter(adapter);
    }
/*
初始化控件
 */
    private void initView() {
        imageClearBtn = (ImageView) findViewById(R.id.image_clear_btn);
        imageSearchBtn = (ImageView) findViewById(R.id.image_search_btn);
        footActivityEdit = (EditText) findViewById(R.id.foot_activity_edit);
        footListview = (ListView) findViewById(R.id.foot_listview);
        onClick();
    }
    /*
    点击事件
     */
    void onClick(){

        //刷新
        imageClearBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                footActivityEdit.setText("");
                datas.clear();
                datas=FoodUtil.getFoodList();
                /*
                当我们把信息存放到一个List链表中时，然后又对这个链表进行流排序，最后去刷新适配器发现无改变，
                原因为咱们改变流链表中值得位置，但没有改变值在内存中所储存的地址，即引用没有改变
                 */
                adapter=new  FootListItemAdapter(food_list_Activity.this,datas);
                footListview.setAdapter(adapter);
            }
        });
        //搜索
        imageSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String info=footActivityEdit.getText().toString().trim();
            if (info.equals("")){
                Toast.makeText(food_list_Activity.this, "请输入内容", Toast.LENGTH_SHORT).show();
            }else {
                foodBeanList.clear();
                for (int i = 0; i < datas.size(); i++) {
                    String title=datas.get(i).getTitle();
                    if (title.contains(info)){
                        foodBeanList.add(datas.get(i));
                    }
                }
                datas.clear();
                datas.addAll(foodBeanList);
                adapter.notifyDataSetChanged();
            }
            }
        });
        //点击item跳转到详细的页面
        footListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(food_list_Activity.this,Food_Details_Activity.class)
                .putExtra("details",datas.get(position)));
            }
        });
    }

}
